<template>
  <!-- 账套表单页面 -->
  <div class="register">
    <el-form
      ref="registerRef"
      :model="Form"
      :rules="registerRules"
      class="register-form"
    >
      <h3 class="title">初始化账套信息</h3>
      <el-form-item prop="accountsName" label="公司名称">
        <el-input
          v-model="Form.accountsName"
          size="large"
          type="text"
          auto-complete="off"
          placeholder="公司名称"
        >
          <template #prefix
            ><svg-icon icon-class="user" class="el-input__icon input-icon"
          /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="accountsType" label="服务类型">
        <el-input
          v-model="Form.accountsType"
          size="large"
          type="text"
          auto-complete="off"
          placeholder="服务类型"
        >
          <template #prefix
            ><svg-icon icon-class="user" class="el-input__icon input-icon"
          /></template>
        </el-input>
      </el-form-item>
      <div class="rk">
        <el-form-item prop="accountsTime" label="启用时间">
          <el-date-picker
            size="large"
            v-model="Form.accountsTime"
            type="date"
            placeholder="请选择时间"
            value-format="YYYY-MM-DD"
          >
          </el-date-picker>
        </el-form-item>
      </div>
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="large"
          type="primary"
          style="width: 100%"
          @click.prevent="handleRegister"
        >
          <span v-if="!loading">保 存</span>
          <span v-else>保 存 中...</span>
        </el-button>
        <div style="float: right">
          <router-link class="link-type" :to="'/register'"
            >返回上一步</router-link
          >
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>Sparke V1.0.0</span>
    </div>
  </div>
</template>

<script setup>
import { saveAccountAPI } from "@/api/system/user.js";
import { useRoute, useRouter } from "vue-router";
import { ElMessageBox } from "element-plus";
import { getCodeImg, register } from "@/api/login";
import useUserStore from "@/store/modules/user";
const userStore = useUserStore();
const route = useRoute();
const router = useRouter();
const { proxy } = getCurrentInstance();
const registerRef = ref(null);
const Form = ref({
  accountsName: "",
  accountsType: "",
  accountsTime: "",
});
const registerRules = {
  accountsName: [
    { required: true, trigger: "blur", message: "请输入您的公司名称" },
  ],
  accountsType: [
    { required: true, trigger: "blur", message: "请输入您的服务类型" },
  ],
  accountsTime: [
    { required: true, trigger: "change", message: "请选择您的启用时间" },
  ],
};

const loading = ref(false);
const captchaEnabled = ref(true);
const redirect = ref(undefined);

function handleRegister() {
  proxy.$refs.registerRef.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      userStore
        .saveAccount({ ...Form.value, phoneNumber: route.query.id })
        .then(() => {
          router.push({ path: redirect.value || "/" });
        })
        .catch(() => {
          loading.value = false;
        });
      // router.push({ path: redirect.value || '/' })
      // register(Form.value)
      //   .then(res => {
      //     const username = Form.value.username
      //     ElMessageBox.alert("<font color='red'>恭喜你，您的账号 " + username + ' 注册成功！</font>', '系统提示', {
      //       dangerouslyUseHTMLString: true,
      //       type: 'success'
      //     })
      //       .then(() => {
      //         router.push('/login')
      //       })
      //       .catch(() => {})
      //   })
      //   .catch(() => {
      //     loading.value = false
      //     if (captchaEnabled) {
      //       getCode()
      //     }
      //   })
    }
  });
}
</script>

<style lang="scss" scoped>
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/bglogin.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 40px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.register-code-img {
  height: 40px;
  padding-left: 12px;
}
.inputButton {
  width: 100%;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  .CodeButton {
    margin-left: 10px;
  }
}
// .register ::v-deep .el-form-item {
//   width: 295px !important;
// }
.rk ::v-deep .el-input__inner {
  width: 100% !important;
}
.rk ::v-deep .el-input {
  width: 100% !important;
}
</style>
